package com.jsrj.controller;

import com.jsrj.common.annotation.Log;
import com.jsrj.common.config.RuoYiConfig;
import com.jsrj.common.core.controller.BaseController;
import com.jsrj.common.core.domain.AjaxResult;
import com.jsrj.common.enums.BusinessType;
import com.jsrj.common.utils.file.FileUploadUtils;
import com.jsrj.domain.Pages;
import com.jsrj.service.IPagesService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.List;

/**
 * 【请填写功能名称】Controller
 * 
 * @author ruoyi
 * @date 2023-01-07
 */
@RestController
@RequestMapping("/system/pages")
public class PagesController extends BaseController
{
    @Autowired
    private IPagesService pagesService;

    /**
     * 查询【请填写功能名称】列表
     */
//    @PreAuthorize("@ss.hasPermi('system:pages:list')")
//    @GetMapping("/list")
//    public TableDataInfo list(Pages pages)
//    {
//        startPage();
//        List<Pages> list = pagesService.selectPagesList(pages);
//        return getDataTable(list);
//    }

//    /**
//     * 导出【请填写功能名称】列表
//     */
//    @PreAuthorize("@ss.hasPermi('system:pages:export')")
//    @Log(title = "【请填写功能名称】", businessType = BusinessType.EXPORT)
//    @PostMapping("/export")
//    public void export(HttpServletResponse response, Pages pages)
//    {
//        List<Pages> list = pagesService.selectPagesList(pages);
//        ExcelUtil<Pages> util = new ExcelUtil<Pages>(Pages.class);
//        util.exportExcel(response, list, "【请填写功能名称】数据");
//    }

    /**
     * 获取【请填写功能名称】详细信息
     * 1
     */
    @PreAuthorize("@ss.hasPermi('system:pages:query')")
    @GetMapping(value = "/selectById/{contentId}")
    public AjaxResult getById(@PathVariable("contentId") Long contentId)
    {
        return AjaxResult.success(pagesService.selectPagesByContentId(contentId));
    }





    /**
     * 新增【请填写功能名称】
     * 1
     */
    @PreAuthorize("@ss.hasPermi('system:pages:add')")
    @Log(title = "【请填写功能名称】", businessType = BusinessType.INSERT)
    @PostMapping("/insert")
    public AjaxResult add(@RequestBody Pages pages)
    {
        pagesService.insertPages(pages);
        return AjaxResult.success(pages.getContentId());
    }

    /**
     * 修改【请填写功能名称】
     * 1
     */
    @PreAuthorize("@ss.hasPermi('system:pages:edit')")
    @Log(title = "【请填写功能名称】", businessType = BusinessType.UPDATE)
    @PutMapping("/update")
    public AjaxResult edit(@RequestBody Pages pages)
    {
        return AjaxResult.success(pagesService.updatePages(pages));
    }

    /**
     * 删除【请填写功能名称】
     * 1
     */
    @PreAuthorize("@ss.hasPermi('system:pages:remove')")
    @Log(title = "【请填写功能名称】", businessType = BusinessType.DELETE)
    @DeleteMapping("/delete")
    public AjaxResult remove(@RequestParam Long contentId)
    {
        return AjaxResult.success(pagesService.deletePagesByContentId(contentId));
    }

    /**
     * 查询全部
     * 1
     * @return
     */
    @PreAuthorize("@ss.hasPermi('system:pages:list')")
    @GetMapping("/selectAll")
    public AjaxResult selectAlly(){
        List<Pages> list = pagesService.selectAll();
        return AjaxResult.success(list);
    }

    /**
     * 上传图片返回url地址
     * @param file
     * @return
     */
    @PostMapping("/insertImg")
    public String insertImg(@RequestParam("file") MultipartFile file){
        // 上传文件路径
        String filePath = RuoYiConfig.getUploadPath();
        // 上传并返回新文件名称
        String fileNewName = null;
        String url = null;
        try {
            fileNewName = FileUploadUtils.upload(filePath, file);
            url = "http://jiangshunznkj.xyz:8080/"+filePath+fileNewName.substring(15);
        } catch (IOException e) {
            e.printStackTrace();
        }
        return url;
    }

}
